<template>
    <div style="width:100%">
        <el-dialog :visible.sync="incomingItemDialogvisible" title="检验项共享数据" width="1000px" append-to-body top="14vh" class="knowledge-dialog" :close-on-click-modal="false" :before-close="close" @close="onClose" @open="onOpen">
            <div>
                <el-form :model="queryParams" size="small" label-position="left" label-suffix=":" ref="queryParams" label-width="80px">
                    <el-row :gutter="24">
                        <el-col :span="7">
                            <el-form-item :label="$t('basicData.baseName')" prop="baseId">
                                <el-select v-model="queryParams.baseId" filterable clearable :placeholder="$t('public.placeholder',{placeholder:$t('basicData.baseName')})" >
                                    <el-option v-for="item in baseList" :key="item.baseId" :label="item.baseName" :value="item.baseId">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item :label="$t('inspectionManagement.plantArea')" prop="factoryCode">
                                <el-select v-model="queryParams.factoryCode" :placeholder="$t('public.select_holder',{select_holder: $t('inspectionManagement.plantArea')})" style="width:100%;" filterable clearable size="small">
                                    <el-option v-for="(item) in factoryList" :key="item.plantCode" :label="item.plantName" :value="item.plantCode" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item :label="$t('mrbForm.MRBTime')" prop="initiatorTime  ">
                                <el-date-picker v-model="initiatorTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" autocomplete="off" range-separator="-" :start-placeholder="$t('public.startDate')" :end-placeholder="$t('public.endDate')"></el-date-picker>
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item :label="$t('inventoryManagement.materialNo')" prop="materialCode">
                                <el-input v-model="queryParams.materialCode" :placeholder="
                            $t('public.placeholder', {
                                placeholder: $t('inventoryManagement.materialNo'),
                            })
                            " size="small" @keyup.enter.native="getList" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item :label="$t('inspectionManagement.materialName')" prop="materialName">
                                <el-input v-model="queryParams.materialName" :placeholder="
                            $t('public.placeholder', {
                                placeholder: $t('inspectionManagement.materialName'),
                            })
                            " size="small" @keyup.enter.native="getList" />
                            </el-form-item>
                        </el-col>
                        <el-col :span="7">
                            <el-form-item :label="$t('inventoryManagement.supplierName')" prop="supplierName">
                                <el-input v-model="queryParams.supplierName" :placeholder="
                            $t('public.placeholder', {
                                placeholder: $t('inventoryManagement.supplierName'),
                            })
                            " size="small" @keyup.enter.native="getList" />
                            </el-form-item>
                        </el-col>
                        <el-col :offset='17' :span="7">
                            <el-form-item style="text-align:right">
                                <el-button type="primary" icon="el-icon-search" class="search-btn" @click="getList" size="mini">{{ $t("public.search") }}</el-button>
                                <el-button icon="el-icon-refresh" @click="reset" size="mini">{{ $t("public.reset") }}</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
                <div class="container-table">
                    <el-table max-height="350" border stripe :data="supplierList" @selection-change="handleSelectionChange">
                        <el-table-column :label="$t('public.serialNum')" type="index" width="50" align="center">
                            <template slot-scope="scope">
                                <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column label="所属基地" align="left" prop="baseName" minWidth="160" />
                        <el-table-column label="所属厂区" align="left" prop="factoryName" minWidth="160" />
                        <el-table-column label="检验时间" align="left" prop="inspectionTime" minWidth="160" />
                        <el-table-column label="物料号" align="left" prop="materialCode" minWidth="160" />
                        <el-table-column label="物料描述" align="left" prop="materialName" minWidth="160" />
                        <el-table-column label="供应商名称" align="left" prop="supplierName" minWidth="160" />
                        <el-table-column label="项目类型" align="left" prop="projectType" minWidth="160" />
                        <el-table-column label="检验项目" align="left" prop="projectName" minWidth="160" />
                        <el-table-column label="抽样方案" align="left" prop="samplingOne" minWidth="160" />
                        <el-table-column label="样本量" align="left" prop="samplingTwo" minWidth="160" />
                        <!-- <el-table-column label="检验结果" align="left" prop="checkoutResult" minWidth="160" /> -->
                        <el-table-column label="检验结果" align="left" width="160">
                            <template slot-scope="scope">
                                <span v-if="scope.row.checkoutResult == 1" class="action-wrap" style="color:#5a9eaf">
                                    合格
                                </span>
                                <span v-if="scope.row.checkoutResult == 0" class="action-wrap" style="color:red">
                                    不合格
                                </span>
                            </template>
                        </el-table-column>
                        <el-table-column label="检验值" align="left" prop="checkoutValuesText" minWidth="160" />
                        <el-table-column fixed="right" label="详情" align="center" width="100">
                            <template slot-scope="scope">
                                <a class="action-wrap" @click="chooseRead(scope)" style="color:#5a9eaf">
                                    查阅
                                </a>
                            </template>
                        </el-table-column>
                        <el-table-column fixed="right" :label="$t('public.opera')" align="center" width="100">
                            <template slot-scope="scope">
                                <a v-if='scope.row.dataType' class="action-wrap" @click="choose(scope)" style="color:#5a9eaf">
                                    已应用
                                </a>
                                <a v-else class="action-wrap" @click="choose(scope)" style="color:#5a9eaf">
                                    应用
                                </a>
                            </template>
                        </el-table-column>
                    </el-table>

                    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" layout="total, sizes, prev, pager, next, jumper" />
                </div>
            </div>

            <div slot="footer">
                <el-button size="mini" type="default" icon="el-icon-close" @click="handleConfirm">{{ $t("btn.close") }}</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { divisionList } from '@/api/masterdata/division'
import { baseList } from '@/api/masterdata/base'
import { listALLFactory } from '@/api/basic/factory'
import { materialInspectionItemList } from '@/api/basic/typeMaterial'
export default {
    name: 'DivisionDialog',
    props: {
        usedTypes:[Object],
        viewModel:[Object],
        incomingItemDialogScope: {
            type: [Object]
        },
        incomingItemDialogvisible: {
            type:Boolean,
            default() {
                return false
            }
        },
        viewtransmissionCurrentType:[Function],
        value: {
            type: [String, Number, Array, Object],
            default() {
                return ''
            },
        },
        divisionId: {
            type: String,
            default: ''
        },
        disabled: {
            type: Boolean,
            default: false,
        },
        params: {
            type: Object,
            default: null,
        },
        clearable:{
            type: Boolean,
            default: true,
        }
    },
    data() {
        return {
            // 日期范围
            initiatorTime: [],
            baseList:[],
            factoryList:[],
            total: 1,
            supplierList: [],
            queryParams: {
                projectCode:'',//当前检验项 检验项目CODE
                projectType:'',//当前检验项 检验类型CODE
                baseMaterialCode:'',//当前检验单物料编号
                baseFactoryCode:'',//当前检验单工厂编号
                samplingOne:'',//当前检验项 抽样方案
                samplingTwo:'',//当前检验项 样本量
                pageNum: 1,
                pageSize: 10
            },
            data: null,
            title: '',
        }
    },
    created(){
        baseList().then((response) => {
            if (response.rows) {
                this.baseList = response.rows
            }
        })
        listALLFactory().then((res) => {
            this.factoryList = res.data
        })

    },
    methods: {
        getList() {
            console.log(this.queryParams,'===========')
            console.log(this.initiatorTime,'选择时间')
            this.queryParams.inspectionStartTime = this.initiatorTime[0]
            this.queryParams.inspectionEndTime = this.initiatorTime[1]
            materialInspectionItemList(this.queryParams).then((response) => {
                if (response.rows) {
                    this.supplierList = response.rows
                    this.total = response.total
                    this.rfreshStatus()
                }
            })
        },
        // 窗口打开
        onOpen() {
            console.log(1,this.incomingItemDialogScope,this.viewModel)
            // 必传筛选字段
            this.queryParams.projectCode = this.incomingItemDialogScope.projectCode ? this.incomingItemDialogScope.projectCode : ''
            this.queryParams.projectType = this.incomingItemDialogScope.projectType ? this.incomingItemDialogScope.projectType : ''
            this.queryParams.baseMaterialCode = this.viewModel.materialCode ? this.viewModel.materialCode : ''
            this.queryParams.baseFactoryCode = this.viewModel.factoryCode ? this.viewModel.factoryCode : ''
            this.queryParams.samplingOne = this.incomingItemDialogScope.samplingOne ? this.incomingItemDialogScope.samplingOne : ''
            this.queryParams.samplingTwo = this.incomingItemDialogScope.samplingTwo ? this.incomingItemDialogScope.samplingTwo : ''
            console.log(2)
            this.getList()
        },
        reset() {
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
            }
            this.queryParams.projectCode = this.incomingItemDialogScope.projectCode ? this.incomingItemDialogScope.projectCode : ''
            this.queryParams.projectType = this.incomingItemDialogScope.projectType ? this.incomingItemDialogScope.projectType : ''
            this.queryParams.baseMaterialCode = this.viewModel.materialCode ? this.viewModel.materialCode : ''
            this.queryParams.baseFactoryCode = this.viewModel.factoryCode ? this.viewModel.factoryCode : ''
            this.queryParams.samplingOne = this.incomingItemDialogScope.samplingOne ? this.incomingItemDialogScope.samplingOne : ''
            this.queryParams.samplingTwo = this.incomingItemDialogScope.samplingTwo ? this.incomingItemDialogScope.samplingTwo : ''
            
            this.getList()
        },
        chooseRead(scope){
            this.$emit('viewtransmissionCurrentType', {item:scope.row,type:'simple'})
        },
        rfreshStatus(){
            // let usedTypes = JSON.parse(localStorage.getItem('usedTypes'))
            console.log(this.usedTypes,'缓存数据共享！！！！！！！！！')
            this.supplierList.forEach(val => {
                if(Object.values(this.usedTypes).includes(val.itemId)){
                    val.dataType = 1
                    console.log(val,'被应用！！！！！！！')
                }else{
                    val.dataType = 0
                }
            })
            this.supplierList = JSON.parse(JSON.stringify(this.supplierList))
            this.$forceUpdate()
        },
        choose(scope) {
            // this.$emit('input',scope.row.divisionName)
            // this.$emit('update:divisionId', scope.row.divisionId)
            // this.$emit('callback', scope.row)
            // this.$emit('update:incomingItemDialogvisible', false)
            this.$emit('viewtransmissionCurrentType', {item:scope.row,type:''})
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.dictId)
            this.single = selection.length != 1
            this.multiple = !selection.length
        },
        // 窗口关闭
        onClose() {},
        handleConfirm() {
            this.$emit('update:incomingItemDialogvisible', false)
        },
        // 窗口关闭前执行
        close() {
            this.$emit('update:incomingItemDialogvisible', false)
        },
        clearCB(){
            this.$emit('input','')
            this.$emit('update:divisionId', '')
            this.$emit('callback', {})
            this.$emit('update:incomingItemDialogvisible', false)
        },
    },
}
</script>

<style scoped>
::v-deep .el-form-item--small .el-form-item__label{
    white-space:nowrap;
}
::v-deep .el-dialog__footer {
    text-align: center;
}
.search-btn {
    background: #20b09f;
    border-color: #20b09f;
}
</style>
